package com.go8.sso.token;

import com.go8.common.util.JsonUtils;
import com.go8.sso.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisTokenCache implements TokenCache {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String USER_TOKEN="user_token:";

    @Override
    public Object getByToken(String token) {
       String m = stringRedisTemplate.opsForValue().get(USER_TOKEN+token);
       return JsonUtils.jsonToPoJo(m, Member.class);
    }

    @Override
    public void setByToken(String token, Object object) {
        stringRedisTemplate.opsForValue().set(USER_TOKEN+token, JsonUtils.objectToJson(object),30, TimeUnit.MINUTES);
    }

    @Override
    public void delByToken(String token) {
        stringRedisTemplate.delete(USER_TOKEN+token);
    }
}
